﻿using Ibp.Service.Entity.DEntity;
using Ibp.Service.Entity.ODAEntity;
using OdaOperation.Core.Creators;
using OdaOperation.DB;
using OdaOperation.Model;
using OdaOperation.Utils;
using Teigha.Core;
using Teigha.TD;

namespace OdaOperation.Extends
{

    /// <summary>
    ///  对数据库中的不同类型的实体进行操作
    /// </summary>
    public static class ExpandEditDbDatabase
    {
        /// <summary>
        ///  为 OdDbEntity 实体设置一个扩展 ID
        /// </summary>
        /// <param name="entity"></param>
        /// <param name="id"></param>
        public static void SetExtendId(this OdDbEntity entity, string id)
        {
            //检查 id 是否为非空和非空白字符串。
            if (!string.IsNullOrWhiteSpace(id))
            {
                //创建一个新的 OdDbField 对象并和。
                OdDbField odDbField = OdDbField.createObject();
                //评估选项
                odDbField.setEvaluationOption(OdDbField.EvalOption.kAutomatic);
                //字段代码
                odDbField.setFieldCode(id, OdDbField.FieldCodeFlag.kTextField);
                //将该字段设置到实体的 QExtendId 字段上
                entity.setField("QExtendId", odDbField);
            }
        }

        /// <summary>
        /// 向数据库中添加一个 OdDbLine 实体
        /// </summary>
        /// <param name="db"></param>
        /// <param name="od"></param>
        /// <param name="css"></param>
        public static void AppendOdDbLine(this DbDatabase db, DLine od, StyleModel css = null)
        {
            //使用委托创建一个新的 OdDbLine 对象，设置起点和终点，线型比例和扩展 ID。
            //将创建的实体返回，并附加到数据库中
            //db.CreateEntity(((DEntityBase)od).Layer, delegate
            db.CreateEntity(((DEntityBase)od).Layer, delegate
            {
                OdDbLine odDbLine = OdDbLine.createObject();
                //设置起点和终点
                odDbLine.setStartPoint(new OdGePoint3d(od.Start.X, od.Start.Y, od.Start.Z));
                odDbLine.setEndPoint(new OdGePoint3d(od.End.X, od.End.Y, od.End.Z));
                //线型比例
                odDbLine.setLinetypeScale(od.LinetypeScale);
                //扩展 ID
                odDbLine.SetExtendId(((DEntityBase)od).ExtendId);
                return odDbLine;
            }, css);
        }

        /// <summary>
        /// 向数据库中添加一个 OdDbArc 实体
        /// </summary>
        /// <param name="db"></param>
        /// <param name="od"></param>
        /// <param name="css"></param>
        public static void AppendOdDbArc(this DbDatabase db, DArc od, StyleModel css = null)
        {
            db.CreateEntity(((DEntityBase)od).Layer, delegate
            {
                OdDbArc odDbArc = OdDbArc.createObject();
                //如果 od 是顺时针方向，则交换起点和终点。
                if (od.IsClockWise)
                {
                    DPoint start = od.Start;
                    DPoint end = od.End;
                    od.Start = (end);
                    od.End = (start);
                }

                odDbArc.SetExtendId(((DEntityBase)od).ExtendId);
                OdGePoint3d odGePoint3d = new OdGePoint3d(od.Center.X, od.Center.Y, od.Center.Z);
                OdGePoint3d p = new OdGePoint3d(od.Start.X, od.Start.Y, od.Start.Z);
                OdGePoint3d p2 = new OdGePoint3d(od.End.X, od.End.Y, od.End.Z);
                OdGeVector3d odGeVector3d = OdaWriteExtend.AngleTetweenVectors(p, odGePoint3d);
                OdGeVector3d odGeVector3d2 = OdaWriteExtend.AngleTetweenVectors(p2, odGePoint3d);
                double startAngle = ((odGeVector3d.y > 0.0) ? OdaWriteExtend.AngleTetweenangleTo(odGeVector3d, OdGeVector3d.kXAxis) : (0.0 - OdaWriteExtend.AngleTetweenangleTo(odGeVector3d, OdGeVector3d.kXAxis)));
                double endAngle = ((odGeVector3d2.y > 0.0) ? OdaWriteExtend.AngleTetweenangleTo(odGeVector3d2, OdGeVector3d.kXAxis) : (0.0 - OdaWriteExtend.AngleTetweenangleTo(odGeVector3d2, OdGeVector3d.kXAxis)));
                //中心
                odDbArc.setCenter(odGePoint3d);
                //起始角
                odDbArc.setStartAngle(startAngle);
                //终止角
                odDbArc.setEndAngle(endAngle);
                double radius = Math.Sqrt(Math.Pow(odGeVector3d.x, 2.0) + Math.Pow(odGeVector3d.y, 2.0));
                //半径
                odDbArc.setRadius(radius);

                //将创建的实体附加到指定的块记录中
                db.Database.GetBlockByName(((DEntityBase)od).BlockName)?.appendOdDbEntity(odDbArc);
                return odDbArc;
            }, css);
        }

        /// <summary>
        /// 向数据库中添加一个 OdDbCircle 实体
        /// </summary>
        /// <param name="db"></param>
        /// <param name="od"></param>
        /// <param name="css"></param>
        public static void AppendOdDbCircle(this DbDatabase db, DCircle od, StyleModel css = null)
        {
            //设置 OdDbCircle 的扩展 ID、中心和半径。
            //将创建的实体附加到指定的块记录中。
            db.CreateEntity(((DEntityBase)od).Layer, delegate
            {
                //在委托内部，创建一个 OdDbCircle 类型的对象 odDbCircle。

                OdDbCircle odDbCircle = OdDbCircle.createObject();

                //设置 odDbCircle 的 ExtendId 属性，其值来自 od 对象的 ExtendId 属性。
                odDbCircle.SetExtendId(((DEntityBase)od).ExtendId);

                //设置 odDbCircle 的中心点，
                //使用 od 对象的 Center 属性（通过 DArc 类型转换得到）的 X、Y 和 Z 坐标创建一个新的 OdGePoint3d 对象。
                odDbCircle.setCenter(new OdGePoint3d(((DArc)od).Center.X, ((DArc)od).Center.Y, ((DArc)od).Center.Z));

                //设置 odDbCircle 的半径，其值来自 od 对象的 Radius 属性。
                odDbCircle.setRadius(((DArc)od).Radius);

                //在 db 的 Database 属性中查找名为 od 对象的 BlockName 属性的块（通过 DEntityBase 类型转换得到），
                //如果找到，则调用 appendOdDbEntity 方法将 odDbCircle 添加到该块中
                db.Database.GetBlockByName(((DEntityBase)od).BlockName)?.appendOdDbEntity(odDbCircle);
                return odDbCircle;
            }, css);
        }

        /// <summary>
        /// 向数据库中添加一个 OdDbEllipse 实体
        /// </summary>
        /// <param name="db"></param>
        /// <param name="od"></param>
        /// <param name="css"></param>
        public static void AppendOdDbEllipse(this DbDatabase db, DEllipse od, StyleModel css = null)
        {
            //设置 OdDbEllipse 的扩展 ID、中心、轴向、长短轴比、起始角和终止角。
            //将创建的实体附加到指定的块记录中。
            db.CreateEntity(((DEntityBase)od).Layer, delegate
            {
                OdDbEllipse odDbEllipse = OdDbEllipse.createObject();
                //设置 扩展 ID
                odDbEllipse.SetExtendId(((DEntityBase)od).ExtendId);
                //设置 中心、轴向、长短轴比、起始角和终止角
                odDbEllipse.set(
                    new OdGePoint3d(od.Center.X, od.Center.Y, od.Center.Z), //中心
                    OdGeVector3d.kZAxis, //轴向
                    new OdGeVector3d(od.MajorAxis.X, od.MajorAxis.Y, od.MajorAxis.Z), od.MinorRadius / od.MajorRadius, od.StartAngle, od.EndAngle); //起始角和终止角
                db.Database.GetBlockByName(((DEntityBase)od).BlockName)?.appendOdDbEntity(odDbEllipse);//将创建的实体附加到指定的块记录中
                return odDbEllipse;
            }, css);
        }

        /// <summary>
        /// 向数据库中添加一个 OdDbPolyline 实体
        /// </summary>
        /// <param name="db"></param>
        /// <param name="od"></param>
        /// <param name="css"></param>
        public static void AppendOdDbPolyline(this DbDatabase db, DPolyline od, StyleModel css = null)
        {
            db.CreateEntity(((DEntityBase)od).Layer, delegate
            {
                OdDbPolyline odDbPolyline = OdDbPolyline.createObject();
                odDbPolyline.SetExtendId(((DEntityBase)od).ExtendId);//扩展 ID、
                if (!string.IsNullOrEmpty(((DEntityBase)od).LineColor)) //颜色
                {
                    string[] array = ((DEntityBase)od).LineColor.Split(',');
                    OdCmColor odCmColor = new OdCmColor();
                    odCmColor.setRGB(byte.Parse(array[0]), byte.Parse(array[1]), byte.Parse(array[2]));
                    odDbPolyline.setColor(odCmColor);
                }

                odDbPolyline.setClosed(od.IsClose);//闭合状态
                for (int i = 0; i < od.Vertexes.Count; i++)//遍历顶点列表，
                {
                    odDbPolyline.addVertexAt(uint.Parse(i.ToString()), new OdGePoint2d(((DPoint)od.Vertexes[i]).X, ((DPoint)od.Vertexes[i]).Y));//添加顶点
                    if (od.Vertexes[i].Bulge != 0.0)//设置折弯（bulge）
                    {
                        double angle = od.Vertexes[i].Bulge / 4.0;
                        double bulge = (od.Vertexes[i].IsClockWise ? (0.0 - Math.Tan(OdaWriteExtend.AngleToRadian(angle))) : Math.Tan(OdaWriteExtend.AngleToRadian(angle)));
                        odDbPolyline.setBulgeAt(uint.Parse(i.ToString()), bulge);
                    }
                }

                //将创建的实体附加到指定的块记录中
                db.Database.GetBlockByName(((DEntityBase)od).BlockName)?.appendOdDbEntity(odDbPolyline);
                return odDbPolyline;
            }, css);
        }

        /// <summary>
        /// 向数据库中添加一个 OdDbText 实体
        /// </summary>
        /// <param name="db"></param>
        /// <param name="od"></param>
        /// <param name="css"></param>
        /// <param name="action"></param>
        public static void AppendOdDbText(this DbDatabase db, DText od, StyleModel css = null, Action<OdDbText> action = null)
        {
            //            扩展方法 AppendOdDbText 向数据库中添加一个 OdDbText 实体。
            //检查文字高度是否大于0。
            //设置 OdDbText 的扩展 ID、文字内容、旋转、对齐方式、样式、位置和高度。
            //将创建的实体附加到指定的块记录中，并执行可选的动作。
            db.CreateEntity(((DEntityBase)od).Layer, delegate
            {
                if (od.Height <= 0.0)
                {
                    throw new Exception("文字高度(Height属性)必须设置！");
                }

                OdDbText odDbText = OdDbText.createObject();
                odDbText.SetExtendId(((DEntityBase)od).ExtendId);//扩展 ID
                odDbText.setTextString(od.Value);//文字内容
                odDbText.setRotation(od.Rotation);//旋转
                odDbText.setVerticalMode(TextVertMode.kTextBottom);//对齐方式
                odDbText.setHorizontalMode(TextHorzMode.kTextCenter);//样式
                odDbText.setAlignmentPoint(new OdGePoint3d(od.Position.X, od.Position.Y, od.Position.Z));
                OdDbObjectId font = TextStyleCreator.GetFont(db.Database, od.FontFamily);
                if (font != null)
                {
                    odDbText.setTextStyle(font);
                }

                odDbText.setPosition(new OdGePoint3d(od.Position.X, od.Position.Y, od.Position.Z));//位置
                odDbText.setHeight(od.Height);//高度

                //将创建的实体附加到指定的块记录中，并执行可选的动作。
                db.Database.GetBlockByName(((DEntityBase)od).BlockName)?.appendOdDbEntity(odDbText);
                action?.Invoke(odDbText);
                return odDbText;
            }, css);
        }

        /// <summary>
        /// 向数据库中添加一个 OdDbMText 实体
        /// </summary>
        /// <param name="db"></param>
        /// <param name="od"></param>
        /// <param name="setAction"></param>
        /// <param name="css"></param>
        public static void AppendOdDbMText(this DbDatabase db, DText od, Action<OdDbMText> setAction = null, StyleModel css = null)
        {
            //设置 OdDbMText 的扩展 ID、位置、内容、宽度、样式、高度和旋转。
            //将创建的实体附加到指定的块记录中，并执行可选的动作。
            db.CreateEntity(((DEntityBase)od).Layer, delegate
            {
                OdDbMText odDbMText = OdDbMText.createObject();
                odDbMText.SetExtendId(((DEntityBase)od).ExtendId);
                odDbMText.setLocation(new OdGePoint3d(od.Position.X, od.Position.Y, od.Position.Z));
                odDbMText.setContents(od.Value);
                odDbMText.setWidth(od.Width);
                OdDbObjectId font = TextStyleCreator.GetFont(db.Database, od.FontFamily);
                if (font != null)
                {
                    odDbMText.setTextStyle(font);
                }

                odDbMText.setTextHeight(od.Height);//高度
                odDbMText.setRotation(od.Rotation);//旋转
                setAction?.Invoke(odDbMText);
                db.Database.GetBlockByName(((DEntityBase)od).BlockName)?.appendOdDbEntity(odDbMText);
                return odDbMText;
            }, css);
        }

        /// <summary>
        /// 调用数据库的方法添加文本样式
        /// </summary>
        /// <param name="db"></param>
        /// <param name="dimTextModel"></param>
        public static void AppendOdDbAlignedTextStyle(this DbDatabase db, DimTextModel dimTextModel)
        {
            //扩展方法 AppendOdDbAlignedTextStyle 向数据库中添加一个对齐的文本样式。
            //调用数据库的方法添加文本样式。
            db.AddTextStyle(dimTextModel);
        }

        /// <summary>
        ///  向数据库中添加一个对齐的尺寸样式
        /// </summary>
        /// <param name="db"></param>
        /// <param name="dimStyleModel"></param>
        /// <param name="dimTextModel"></param>
        public static void AppendOdDbAlignedDimensionStyle(this DbDatabase db, DimStyleModel dimStyleModel, DimTextModel dimTextModel = null)
        {
            //            扩展方法 AppendOdDbAlignedDimensionStyle 向数据库中添加一个对齐的尺寸样式。
            //如果文本样式不存在，则添加文本样式。
            //设置尺寸样式的文本样式名称，并调用数据库的方法添加尺寸样式。
            if (dimTextModel == null)
            {
                dimTextModel = new DimTextModel();
            }

            if (!db.GetIsHaveTextStyle(dimTextModel.Name))
            {
                db.AddTextStyle(dimTextModel);
            }

            dimStyleModel.DimtxstyName = dimTextModel.Name;
            db.AddDimStyle(dimStyleModel);
        }

        /// <summary>
        /// 向数据库中添加一个对齐的尺寸实体
        /// </summary>
        /// <param name="db"></param>
        /// <param name="odDimension"></param>
        /// <param name="isBlock"></param>
        /// <param name="action"></param>
        public static void AppendOdDbAlignedDimension(this DbDatabase db, OdDimensionModel odDimension, bool isBlock = false, Action<OdDbAlignedDimension> action = null)
        {
            //创建并设置 OdDbAlignedDimension 的各个属性。
            //如果指定的文本层名称为空，直接附加实体到数据库。
            //如果存在维度样式，设置其样式。
            //根据提供的爆炸文本层名称处理文本层。
            //执行可选的动作。
            db.CreateEntity(delegate
            {
                OdDbAlignedDimension odDbAlignedDimension = OdDbAlignedDimension.createObject();
                odDbAlignedDimension.SetExtendId(odDimension.ExtendId);
                odDbAlignedDimension.setDatabaseDefaults(db.Database);
                if (string.IsNullOrEmpty(odDimension.ExplodeTextLayerName))
                {
                    db.AppendEntityToDatabase(db.Database, odDbAlignedDimension);
                }

                if (db.GetIsHaveDimStyle(odDimension.DimStyleName))
                {
                    odDbAlignedDimension.setDimensionStyle(db.GetIsdimStyleOBid(odDimension.DimStyleName));
                }

                odDbAlignedDimension.setXLine1Point(new OdGePoint3d(odDimension.Line1Point.X, odDimension.Line1Point.Y, odDimension.Line1Point.Z));
                odDbAlignedDimension.setXLine2Point(new OdGePoint3d(odDimension.Line2Point.X, odDimension.Line2Point.Y, odDimension.Line2Point.Z));
                if (!string.IsNullOrEmpty(odDimension.DimensionText))
                {
                    odDbAlignedDimension.setDimensionText(odDimension.DimensionText);
                }

                odDbAlignedDimension.setDimLinePoint(new OdGePoint3d(odDimension.DimLinePoint.X, odDimension.DimLinePoint.Y, odDimension.DimLinePoint.Z));
                if (!string.IsNullOrEmpty(odDimension.ExplodeTextLayerName))
                {
                    if (!db.GetLayerState(odDimension.ExplodeTextLayerName))
                    {
                        db.AddNewLayer(odDimension.ExplodeTextLayerName);
                    }

                    OdRxObjectPtrArray odRxObjectPtrArray = new OdRxObjectPtrArray();
                    odDbAlignedDimension.explode(odRxObjectPtrArray);
                    if (odRxObjectPtrArray.Count > 0)
                    {
                        for (int i = 0; i < odRxObjectPtrArray.Count; i++)
                        {
                            OdRxObject odRxObject = odRxObjectPtrArray[i];
                            (odRxObject as OdDbEntity).setDatabaseDefaults(db.Database);
                            db.AppendEntityToDatabase(db.Database, odRxObject as OdDbEntity);
                            OdDbMText odDbMText = odRxObject as OdDbMText;
                            if (odDbMText != null)
                            {
                                odDbMText.setLayer(odDimension.ExplodeTextLayerName);
                            }
                            else
                            {
                                (odRxObject as OdDbEntity).setLayer(odDimension.LayerName);
                            }
                        }
                    }
                }
                else
                {
                    odDbAlignedDimension.setLayer(odDimension.LayerName, doSubents: false);
                }

                action?.Invoke(odDbAlignedDimension);
            });
        }

        /// <summary>
        /// 向数据库中添加一个多引线标注实体
        /// </summary>
        /// <param name="db"></param>
        /// <param name="leader"></param>
        /// <param name="action"></param>
        public static void AppendOdDbMLeader(this DbDatabase db, LeaderModel leader, Action<OdDbMLeader> action = null)
        {
            /// 创建并设置 OdDbMLeader 的各个属性和箭头符号
            /// 创建并设置 OdDbMText 作为引线文本内容
            db.CreateEntity(delegate
            {
                OdDbMLeader odDbMLeader = OdDbMLeader.createObject();
                odDbMLeader.setDatabaseDefaults(db.Database);
                db.AppendEntityToDatabase(db.Database, odDbMLeader);
                odDbMLeader.setLeaderLineColor(Common.RgbToOdCmColor(leader.LineColor));
                odDbMLeader.setLayer(leader.LayerName, doSubents: false);
                odDbMLeader.setScale(100.0);
                if (!string.IsNullOrEmpty(leader.ArrowSymbolName))
                {
                    OdDbObjectId arrowSymbolId;
                    if (!db.GetBlockState(leader.ArrowSymbolName))
                    {
                        OdDbBlockTable odDbBlockTable = OdDbBlockTable.cast(db.Database.getBlockTableId().openObject(OpenMode.kForRead));
                        OdDbBlockTableRecord odDbBlockTableRecord = OdDbBlockTableRecord.createObject();
                        odDbBlockTableRecord.setName(leader.ArrowSymbolName);
                        odDbBlockTableRecord.setComments("Block for MLeader arrow heads.");
                        odDbBlockTable.upgradeOpen();
                        arrowSymbolId = odDbBlockTable.add(odDbBlockTableRecord);
                        odDbBlockTable.downgradeOpen();
                    }
                    else
                    {
                        arrowSymbolId = db.GetBlockOdDbObjectId(leader.ArrowSymbolName);
                    }

                    odDbMLeader.setArrowSymbolId(arrowSymbolId);
                }
                //创建并设置 OdDbMText 作为引线文本内容
                OdDbMText odDbMText = OdDbMText.createObject();
                odDbMText.setDatabaseDefaults(db.Database);
                odDbMText.setLocation(leader.LeaderLine.ToGePoint3D());
                odDbMText.setContents(leader.TextContent);
                odDbMText.setTextHeight(leader.TextHeight);
                odDbMLeader.setMText(odDbMText);
                odDbMText.setColor(Common.RgbToOdCmColor("ByLayer"));
                odDbMLeader.setTextAttachmentType(OdDbMLeaderStyle.TextAttachmentType.kAttachmentBottomLine, OdDbMLeaderStyle.LeaderDirectionType.kLeftLeader);
                odDbMLeader.setTextAttachmentType(OdDbMLeaderStyle.TextAttachmentType.kAttachmentBottomLine, OdDbMLeaderStyle.LeaderDirectionType.kRightLeader);
                odDbMLeader.setExtendLeaderToText(bSet: true);
                odDbMLeader.setDoglegLength(0.0);
                odDbMLeader.addLeaderLine(leader.FirstVertex.ToGePoint3D(), out var _);
                odDbMLeader.setDoglegLength(1.0);
                odDbMLeader.setDoglegLength(0.0);
                action?.Invoke(odDbMLeader);
            });
        }

        /// <summary>
        /// 获取线型状态
        /// </summary>
        /// <param name="db"></param>
        /// <param name="lineTypeName"></param>
        /// <returns></returns>
        public static bool GetLineTypeState(this DbDatabase db, string lineTypeName)
        {
            return db.LineTypeState(lineTypeName);
        }

        /// <summary>
        /// 添加新的线型
        /// </summary>
        /// <param name="db"></param>
        /// <param name="path"></param>
        /// <param name="linTypeName"></param>
        public static void AddNewLineType(this DbDatabase db, string path, params string[] linTypeName)
        {
            db.AddLineType(path, linTypeName);
        }

        /// <summary>
        /// 设置图层的线型、线宽和颜色
        /// </summary>
        /// <param name="db"></param>
        /// <param name="layerName"></param>
        /// <param name="lineTypeName"></param>
        /// <param name="weight"></param>
        /// <param name="lineColorRgb"></param>
        public static void SetLayerOfLineSomething(this DbDatabase db, string layerName, string lineTypeName = null, LineWeight weight = LineWeight.kLnWtByLwDefault, string lineColorRgb = null)
        {
            db.SetLineTypeOnLayer(layerName, lineTypeName, weight, lineColorRgb);
        }

        /// <summary>
        /// 检查块名称状态
        /// </summary>
        /// <param name="db"></param>
        /// <param name="odBlock"></param>
        /// <returns></returns>
        public static bool GetBlockNameState(this DbDatabase db, OdBlockModel odBlock)
        {
            return db.GetBlockState(odBlock.BlockName);
        }

        /// <summary>
        /// 从文件中复制块
        /// </summary>
        /// <param name="db"></param>
        /// <param name="odBlock"></param>
        /// <exception cref="Exception"></exception>
        public static void CopyBlockFromFile(this DbDatabase db, OdBlockModel odBlock)
        {
            if (string.IsNullOrEmpty(odBlock.FileName) || string.IsNullOrEmpty(odBlock.BlockName))
            {
                throw new Exception("BlockName,FileName 两个参数不能为空！");
            }

            db.CopyBlockFromFile(odBlock);
        }

        /// <summary>
        /// 导入块文件
        /// </summary>
        /// <param name="db"></param>
        /// <param name="blockName"></param>
        /// <param name="fileName"></param>
        /// <param name="isExitSkip"></param>
        public static void ImportBlockFile(this DbDatabase db, string blockName, string fileName, bool isExitSkip = false)
        {
            MemoryTransaction value = MemoryManager.GetMemoryManager().StartTransaction(Thread.CurrentThread.ManagedThreadId);
            try
            {
                OdDbBlockTable odDbBlockTable = (OdDbBlockTable)db.Database.getBlockTableId().safeOpenObject(OpenMode.kForWrite);
                if (!odDbBlockTable.has(blockName))
                {
                    ExHostAppServices exHostAppServices = new ExHostAppServices();
                    OdDbDatabase pSource = exHostAppServices.readFile(fileName);
                    OdDbObjectId odDbObjectId = db.Database.insert(blockName, pSource);
                }
                else if (!isExitSkip)
                {
                    throw new Exception("块名称已经存在！");
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
            finally
            {
                MemoryManager.GetMemoryManager().StopTransaction(value);
            }
        }

        /// <summary>
        /// 添加新的图层
        /// </summary>
        /// <param name="db"></param>
        /// <param name="layerName"></param>
        public static void AddNewLayer(this DbDatabase db, string layerName)
        {
            db.AddLayer(layerName);
        }

        /// <summary>
        /// 获取缩略图位图
        /// </summary>
        /// <param name="db"></param>
        /// <returns></returns>
        public static string GetThumbnailBitmap(this DbDatabase db)
        {
            return "";
        }

        /// <summary>
        /// 将字节数组转换为图像并保存到文件
        /// </summary>
        /// <param name="buffer"></param>
        public static void BytesToImage(byte[] buffer)
        {
            FileStream fileStream = new FileStream("D:\\inf.png", FileMode.Create, FileAccess.Write);
            fileStream.Write(buffer, 0, buffer.Length);
            fileStream.Close();
        }
    }
}